//
//  MineViewController.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/17.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

class MineViewController: TTBaseVC {

    let MineCellID = "MineCell"

    let arr = ["我的钱包","帮助与反馈"]
    lazy var headImage:UIImageView = {
        let image = UIImageView.init(frame: FRAME_CGFloat(17.5, 17.5, 64, 64))
        image.layer.cornerRadius = 32
        image.layer.masksToBounds = true
        image.image = IMAGE(name: "Mine_head")
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(0, 0, 64, 64)
        btn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        return image
    }()
    
    lazy var headLabel:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(91.5, 25.5, 200, 26.5))
        label.text = "登录"
        label.font = Font_Medium(19)
        label.textColor = RGB_Same(53)
        return label
    }()
    
    lazy var headBtn:UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(91.5, 25.5, 200, 26.5)
        btn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var headBtn1:UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(17.5, 17.5, 64, 64)
        btn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var signatureLabel:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(91.5, 59.5, SCREEN_WIDTH - 100, 20))
        label.text = "天天必听是我最喜欢的伴侣"
        label.font = FONT(14)
        label.textColor = RGB_Same(53)
        return label
        
    }()
    
    lazy var tableView: UITableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.bounces = true
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(NIBNAME(MineCellID), forCellReuseIdentifier: MineCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "我的"
        setTopRightView()
        setTopView()
        setUI()
        setData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setUserInfor()
    }
    
    func setUI() {
        self.view.addSubview(self.tableView)
        
    }
    
    func setTopRightView() {
        let contentView = UIView.init(frame: FRAME_CGFloat(0, 0, 44, 44))
        let bt = UIButton.init(frame: contentView.bounds)
        contentView.addSubview(bt)
        bt.setImage(IMAGE(name: "mine消息1"), for: .normal)
        bt.addTarget(self, action: #selector(rightBtnAction), for: .touchUpInside)
        let rightBtn = UIBarButtonItem.init(customView: contentView)
        self.navigationItem.rightBarButtonItem = rightBtn
        
    }
    
    func setTopView() {
        let topView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 150))
        topView.backgroundColor = RGB_Same(243)
        self.tableView.tableHeaderView = topView
        topView.addSubview(headImage)
        topView.addSubview(headLabel)
        topView.addSubview(headBtn)
        topView.addSubview(headBtn1)
//        topView.addSubview(signatureLabel)
    }
    
    func setData() {
        
    }
    
    func setUserInfor() {
        MineRequestModel.loadUserInfo(parameters: ["userId":String.getUserId()]) { (success, message, model) in
            if success && model != nil {
                self.headLabel.text = model?.wxNickname
                self.headImage.sd_setImage(with: URLSTR(model?.wxAvatarUrl ?? ""), placeholderImage: IMAGE(name: "Mine_head"), options: .lowPriority, completed: nil)
                UserDefaults.standard.set(model?.id, forKey: "userId")
            }
        }
    }
    
    @objc func btnAction(btn: UIButton) {
        if btn.tag == 11{
            
        } else if btn.tag == 12 {
            
        }
        
    }
    
    @objc func loginAction() {
        if String.getUserIdToString() {
            if String.getUserId() == 46158 {
                
            } else {
                TTAlertView.ask(title: nil, question: "是否退出登录", leftTitle: "取消", rightTitle: "是的", okBlock: {
                    UserDefaults.standard.removeObject(forKey: UserId)
                    UserDefaults.standard.removeObject(forKey: WxNickname)
                    UserDefaults.standard.removeObject(forKey: WxAvatarUrl)
                    self.headLabel.text = "登录"
                    self.headImage.image = IMAGE(name: "Mine_head")
                }, noBlock: {
                    
                })
            }
        } else {
            let vc = LoginVc()
            self.present(vc, animated: true) {
                
            }
        }
    }
}

// MARK: *******  事件  ********
extension MineViewController {
    @objc func rightBtnAction() {
        let vc = NewsVc()
        ISLOGIN_PUSHVIEWCONTROLLER(vc)
    }
}

// MARK: *******  协议  ********
extension MineViewController: UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MineCellID) as! MineCell
        cell.titleLabel.text = arr[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            let vc = WalletVc()
            ISLOGIN_PUSHVIEWCONTROLLER(vc)
        case 1:
            let vc = HelpVc()
            ISLOGIN_PUSHVIEWCONTROLLER(vc)
        case 2:
            let vc = HelpVc()
            ISLOGIN_PUSHVIEWCONTROLLER(vc)
        default:
            return
        }
    }
    
}
